package com.donfu.web.controller.house;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.donfu.common.annotation.Log;
import com.donfu.common.core.controller.BaseController;
import com.donfu.common.core.domain.AjaxResult;
import com.donfu.common.enums.BusinessType;
import com.donfu.system.domain.house.HouseShoppers;
import com.donfu.system.service.house.IHouseShoppersService;
import com.donfu.common.utils.poi.ExcelUtil;
import com.donfu.common.core.page.TableDataInfo;

/**
 * 导购信息Controller
 * 
 * @author donfu
 * @date 2021-12-01
 */
@Api(tags = "导购信息")
@RestController
@RequestMapping("/house/shoppers")
public class HouseShoppersController extends BaseController
{
    @Autowired
    private IHouseShoppersService houseShoppersService;

    /**
     * 查询导购信息列表
     */
    @ApiOperation("查询信息列表，无店铺名称")
    @PreAuthorize("@ss.hasPermi('house:shoppers:list')")
    @GetMapping("/list")
    public TableDataInfo list(HouseShoppers houseShoppers)
    {
        startPage();
        List<HouseShoppers> list = houseShoppersService.selectHouseShoppersList(houseShoppers);
        return getDataTable(list);
    }

    /**
     * 查询导购信息列表
     */
    @ApiOperation("查询信息列表，含店铺名称")
    @PreAuthorize("@ss.hasPermi('house:shoppers:list')")
    @GetMapping("/listHaveStoreName")
    public TableDataInfo listHaveStoreName(HouseShoppers houseShoppers)
    {
        startPage();
        List<HouseShoppers> list = houseShoppersService.selectHouseShoppersListHaveStoreName(houseShoppers);
        return getDataTable(list);
    }

    /**
     * 导出导购信息列表
     */
    @ApiOperation("导出导购信息")
    //@PreAuthorize("@ss.hasPermi('house:shoppers:export')")
    @Log(title = "导购信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HouseShoppers houseShoppers)
    {
        List<HouseShoppers> list = houseShoppersService.selectHouseShoppersList(houseShoppers);
        ExcelUtil<HouseShoppers> util = new ExcelUtil<HouseShoppers>(HouseShoppers.class);
        return util.exportExcel(list, "导购信息数据");
    }

    /**
     * 获取导购信息详细信息
     */
    @ApiOperation("获取导购的详细信息")
    @PreAuthorize("@ss.hasPermi('house:shoppers:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(houseShoppersService.selectHouseShoppersById(id));
    }

    /**
     * 新增导购信息
     */
    @ApiOperation("新增导购信息")
    @PreAuthorize("@ss.hasPermi('house:shoppers:add')")
    @Log(title = "导购信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody HouseShoppers houseShoppers)
    {
        AjaxResult ajaxResult = checkHouseShoppers(houseShoppers);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseShoppersService.insertHouseShoppers(houseShoppers));
    }

    /**
     * 修改导购信息
     */
    @ApiOperation("修改导购信息")
    @PreAuthorize("@ss.hasPermi('house:shoppers:edit')")
    @Log(title = "导购信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseShoppers houseShoppers)
    {
        Long id = houseShoppers.getId();
        if(id==null){
            return AjaxResult.error("id不能为空");
        }
        AjaxResult ajaxResult = checkHouseShoppers(houseShoppers);
        String code = ajaxResult.get("code").toString();
        if(code.contains("50")){
            return ajaxResult;
        }
        return toAjax(houseShoppersService.updateHouseShoppers(houseShoppers));
    }

    /**
     * 删除导购信息
     */
    @ApiOperation("删除导购信息")
    @PreAuthorize("@ss.hasPermi('house:shoppers:remove')")
    @Log(title = "导购信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(houseShoppersService.deleteHouseShoppersByIds(ids));
    }

    /**
     * 根据店铺id获取导购人员列表
     * @param id
     * @return
     */
    @ApiOperation("根据店铺id获取导购人员列表")
    @GetMapping("/getListByStoreId/{id}")
    public TableDataInfo getListByStoreId(@PathVariable("id") Long id){
        startPage();
        List<HouseShoppers> list=houseShoppersService.selectHouseShoppersListByStoreId(id);
        return getDataTable(list);
    }

    /**
     * 检测houseShoppers字段是否合理
     * @param houseShoppers
     * @return
     */
    public AjaxResult checkHouseShoppers(HouseShoppers houseShoppers){
        Long storeId = houseShoppers.getStoreId();
        if(storeId==null || storeId.equals("")){
            return AjaxResult.error("所属店铺需要选择");
        }
        String name = houseShoppers.getName();
        if(name==null || name.equals("")){
            return  AjaxResult.error("名称不能为空");
        }
        if(name!=null && name.length()>50){
            return AjaxResult.error("名称太长了");
        }
        String constellation = houseShoppers.getConstellation();
        if(constellation==null || constellation.equals("")){
            return AjaxResult.error("星座不能为空");
        }
        String zodiac = houseShoppers.getZodiac();
        if(zodiac==null || zodiac.equals("")){
            return AjaxResult.error("生肖不能为空");
        }
        String motto = houseShoppers.getMotto();
        if(motto==null || motto.equals("")){
            return AjaxResult.error("工作格言不能为空");
        }
        if(motto.length()>100){
            return AjaxResult.error("工作格言不能超过100个字符");
        }
        String qrcode = houseShoppers.getQrcode();
        if(qrcode==null || qrcode.equals("")){
            return AjaxResult.error("导购微信不能为空");
        }
        String phone = houseShoppers.getPhone();
        if(phone==null || phone.equals("")){
            return AjaxResult.error("手机号码不能空");
        }
        String regexPhone = "^(1[0-9])\\d{9}$";
        boolean matches = phone.matches(regexPhone);
        if (!matches) {
            return AjaxResult.error("手机号码格式不正确");
        }
        String standby = houseShoppers.getStandby();
        if(standby!=null && standby.length()>100){
            return AjaxResult.error("备注太长了");
        }
        return AjaxResult.success();
    }
}
